package com.waverider.promotion.web.bg.screen;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.iacrqq.util.StringUtil;
import com.waverider.promotion.biz.manager.OrderManager;
import com.waverider.promotion.domain.OrderDO;
import com.waverider.promotion.model.BaseQueryModel;
import com.waverider.promotion.model.OrderQueryModel;
import com.waverider.promotion.model.ResultModel;
import com.waverider.promotion.web.AbstractLoginedController;

public class OrderList extends AbstractLoginedController
{

	private static final Log logger = LogFactory.getLog(OrderList.class);
	
	private OrderManager orderManager;

	@Override
	protected ModelAndView handleLogined(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		HttpSession session = request.getSession();
		Long currentPage = null;
		String strCurrentPage = request.getParameter("current_page");
		if (StringUtil.isBlank(strCurrentPage))
		{
			currentPage = 1L;
		}
		else
		{
			try
			{
				currentPage = Long.valueOf(strCurrentPage);
			}
			catch (NumberFormatException e)
			{
				logger.error(e);
				currentPage = 1L;
			}
		}
		
		String strBuyerNick =  request.getParameter("buyer_nick");
		strBuyerNick = StringUtil.isBlank(strBuyerNick) ? null : strBuyerNick;
		String strStatus = request.getParameter("status");
		Integer status = StringUtil.isBlank(strStatus) ? null : Integer.valueOf(strStatus);
		
		OrderQueryModel queryModel = OrderQueryModel.buildQueryModel(getShopId(session), strBuyerNick, status, currentPage, BaseQueryModel.DEFAULT_PAGE_SIZE);
		ResultModel<OrderDO> resultModel = orderManager.getOrderList(queryModel);

		ModelAndView mv = new ModelAndView("bg/orderList");
		Map<String, Object> context = mv.getModel();
		context.put("resultModel", resultModel);
		context.put("pageNo", 3);
		return mv;
	}

	public void setOrderManager(OrderManager orderManager)
	{
		this.orderManager = orderManager;
	}
}
